<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');
class IndexController extends BaseController {
	
	public function error(){
		$this->display();
	}
	
    public function index(){
    	if(C('is_open')==0){
    		$this->redirect('error');
    	}
    	$t_id = I('t');
		session('tid',$t_id);
		$user = session('user');
    	$config = C('WEIXINPAY_CONFIG');
    	

    				
					
					
				


		// $res = check_format("2/龙/300");
		//                     // $pk10data = F('pk10data');
		// 					$map['userid'] = $user['id'];
		// 					$map['number'] = "637494";
		// 					$map['type'] = $res['type'];
		// 					$map['state'] = 1;
		// 					$map['time'] = time();
		// 					$map['jincai'] = "3/龙/300";
		// 					$map['is_add'] = 0;
		// 					$map['add_points'] = 0;
		// 					$map['del_points'] = $res['points'];
		// 					$map['balance'] = $user['points']-$map['del_points'];
		// 					$map['nickname'] = $user['nickname'];
		// 					$res2 = M('order')->add($map);
		// 					echo $res2;
		// 					// var_dump($res);
		// 					exit();
		// 			        if($res2){
		// 				    $this->success('添加成功!',U('Home/Index/index'),1);
		// 			        }else{
		// 				    $this->error('添加失败');
		// 			        }
	}	
	
	public function redirect_url(){
		$config = C('WEIXINPAY_CONFIG');
		$oauth = load_wechat('Oauth');
		$result = $oauth->getOauthAccessToken();
		$userinfo = $oauth->getOauthUserinfo($result['access_token'], $result['openid']);
		
		//判断是否第一次登陆
		$wx = M('wx');
		$user = M('user');
		$res = $wx->where("openid = '{$result['openid']}'")->find();
		if($res){
			//是否过期
			if($res['expires_in']<time()){
				$wx->where("openid = '{$result['openid']}'")->setField('access_token',$result['access_token']);
			}
			//查找会员数据
			$info = $user->where("id = {$res['userid']}")->find();
			session('user',$info);
			
			//是否禁用
			if($info['status']==0){
				$this->redirect('error');
			}
			
			//是否有二维码
			if(!$res['qrcode']){
				if($res['t_id']){//一级分销
					$siteurl = $_SERVER['SERVER_NAME'];
					$url = 'http://'.$siteurl;
					$img = qrcode($url);
				}else{
					$siteurl = $_SERVER['SERVER_NAME'];
					$url = 'http://'.$siteurl.'?t='.$info['id'];
					$img = qrcode($url);
				}
				$user->where("id = {$res['userid']}")->setField('qrcode','http://'.$siteurl.'/'.$img);
			}
			$this->redirect('Home/Circle/index');
		}else{
			if(C('is_open_reg')==0){
	    		$this->redirect('error');
	    	}
			
			//是否推荐
			$t_id = session('tid');
			if($t_id){
				$data['t_id'] = $t_id;
			}
			//自动注册
			$data['nickname'] = $userinfo['nickname'];
			$headimgurl = substr($userinfo['headimgurl'], 0,-2);
			$data['headimgurl'] = $headimgurl.'/46';
			$data['country'] = $userinfo['country'];
			$data['province'] = $userinfo['province'];
			$data['sex'] = $userinfo['sex'];
			$data['user_agent'] = serialize(get__browser());
			$data['city'] = $userinfo['city'];
			$data['reg_ip'] = get_client_ip();
			$data['last_ip'] = get_client_ip();
			$data['reg_time'] = time();
			$data['last_time'] = time();
			$data['logins'] = 1;
			$userid = $user->add($data);
			if($userid){
				if($t_id){
					//推荐码（二维码）
					$siteurl = $_SERVER['SERVER_NAME'];
					$url = 'http://'.$siteurl;
					$img = qrcode($url);
				}else{
					$siteurl = $_SERVER['SERVER_NAME'];
					$url = 'http://'.$siteurl.'?t='.$userid;
					$img = qrcode($url);
				}
				$user->where("id = $userid")->setField('qrcode','http://'.$siteurl.'/'.$img);
				
				$data1['userid'] = $userid;
				$data1['openid'] = $result['openid'];
				$data1['access_token'] = $result['access_token'];
				$data1['expires_in'] = time()+$result['expires_in'];
				$res2 = $wx->add($data1);
				if($res2){
					$data['id'] = $userid;
					session('user',$data);
					$this->redirect('Home/Circle/index');
				}
			}
		}
	}	
	
}